<template>
  <div >
    <i class="el-icon-s-home" @click="back()"></i>
    <h1>注册</h1>
  <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="用户名" prop="name">
      <el-input v-model="ruleForm.username" auto-complete="off"></el-input>
    </el-form-item>
    <el-form-item label="登录密码" prop="password">
      <el-input type="password" v-model="ruleForm.password" placeholder="请输入密码" auto-complete="off"></el-input>
    </el-form-item>
    <el-form-item label="昵称" prop="alias">
      <el-input v-model="ruleForm.alias"></el-input>
    </el-form-item>
    <el-form-item label="电话" prop="phone">
      <el-input v-model="ruleForm.phone"></el-input>
    </el-form-item>
    <el-form-item label="身份证号码" prop="idCard">
      <el-input v-model="ruleForm.idCard"></el-input>
    </el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">立即注册</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        ruleForm: {
          username: '',
          password: '',
          alias: '',
          phone: '',
          idCard: ''
        },
        rules: {
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
            { min: 9, max: 15, message: '长度在 9 到 15 个字符,只能英文字母或数字搭配', trigger: 'blur',pattern:/^[a-zA-Z0-9]+$/ }
          ],
          password: [
            { required: true, message: '请输入您的密码', trigger: 'blur' },
            { min: 9, max: 15, message: '长度在 9 到 15 个字符,只能英文字母或数字搭配', trigger: 'blur',pattern:/^[a-zA-Z[0-9]+$/ }
          ],
          alias: [
            { required: true, message: '请输入昵称', trigger: 'blur' },
            { max: 24, message: '最多只能8个汉字', trigger: 'blur',pattern:/^[a-zA-Z0-9\u4e00-\u9fa5]+$/ }
          ],
          phone: [
            { required: true, message: '请输入您的手机号码', trigger: 'blur' },
            { message: '请输入正确的手机号', trigger: 'blur',pattern:/^1[3456789]\d{9}$/}
          ],
          idCard: [
            { required: true, message: '请输入身份证号', trigger: 'blur' },
            { min: 9, max: 15, message: '请输入18位身份证号码', trigger: 'blur',pattern:/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/ }
          ]
        },

      }
    },
    methods: {
      async submitForm(forName){

        const {data:result} = await this.$http.post('/user/register',this.ruleForm)
        if(result.status !== 200) return this.$message.error("该用户名已存在，注册失败")
        this.$message.success("用户注册成功")
        this.$router.push("/login")
      },
      resetForm() {
        this.ruleForm = {}
      },
      back(){
        this.$router.push("/login")
      }
    }
  }
</script>

<style>
</style>
